//
// Copyright (c) 2024 BPer
//
#ifndef __BPerAudioDecoderHandles__
#define __BPerAudioDecoderHandles__

#include <bperutil/sys/BPerThread2.h>
#include <bperutil/buffer/BPerAudioEncoderBuffer.h>
#include <bperutil/buffer/BPerAudioPlayBuffer.h>
#include <bperdecoder/BPerAudioDecoder.h>
#include <vector>
#include <map>

using namespace std;
class BPerAudioDecoderHandles:public BPerThread,public BPerDecoderCallback
{
public:
	BPerAudioDecoderHandles(BPerContext *pcontext);
	~BPerAudioDecoderHandles(void);

private:
	int32_t m_isInit;

public:
	BPerAudioDecoder *getDecoder(int32_t puid);
	BPerAudioPlayBuffer* getAudioBuffer(int32_t puid);
	int32_t getDecoderIndex(int32_t puid);
	void init();
	void stop();
	void setInAudioBuffer(BPerAudioEncoderBuffer *pbuf);
	void setOutAudioBuffer(vector<BPerAudioPlayBuffer*>* pbuf);
	void removeAudioStream(int32_t puid);
	void removeAllStream();
	int32_t m_isStart;
	void onAudioData(BPerFrame* pframe);
	void onVideoData(BPerFrame* pframe);
	void setRemoteParam(int32_t puid,BPerAudioParam* para);

protected:
	void run();
	void stopLoop();
	void startLoop();

void saveFile(char *fileName, uint8_t *pBuffer, long BufferLen);

private:
	std::map<int,BPerAudioParam*> m_paramMap;
	vector<BPerAudioPlayBuffer*> *m_out_audioBuffer;
	vector<BPerAudioDecoder*> *m_decs;
	int32_t m_isConvert;
	BPerAudioEncoderBuffer *m_in_audioBuffer;
	BPerContext *m_context;

	int32_t m_frameSize,m_channel;
	void decode(int32_t isIframe,uint8_t*src,int32_t  p_buflen,uint8_t *dest,int32_t *p_destLen);

};
#endif
